﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
/*
 * 类的索引器测试
 */
namespace ConsoleTest.ChapterEleven
{
    /// <summary>
    /// 索引器
    /// </summary>
    class Index
    {
        string[] _names;
        public Index(params string[] names)
        {
            _names = new string[names.Length];
            names.CopyTo(_names, 0);
        }
        //自定义索引器，索引器可以接受一个或者多个任意类型参数
        //字符串、整数、任何类型对象等等
        public int this[string name]
        { 
            //索引器同样可以定义get set 属性
            get { return Array.IndexOf(_names, name); }
        }

        public string this[int index]
        {
            get { return _names[index]; }
            set { _names[index]=value;}
        }

        public static void IndexerTest()
        {
            Index arr = new Index("Jenny", "Keiwn", "Carl");
            Console.WriteLine(arr[0]);
            int index = arr["Keiwn"];
            arr[index] = "Kewin Still";
            Console.WriteLine(arr[index]);
            Console.ReadLine();
        }
    }
}
